package com.example.redistest;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;

import java.util.Set;

public class RedisDemo {
    public static void test1(Jedis jedis){
        jedis.flushDB();
        jedis.set("key1", "1");
        jedis.set("key2", "2");
        // 配置其他参数
        SetParams setParams = new SetParams();
        setParams.ex(10);
        setParams.xx();
        jedis.set("key1", "111", setParams);
        String res = jedis.get("key1");
        System.out.println(res);
    }
    public static void test2(Jedis jedis){
        jedis.flushDB();
        jedis.mset("key1", "val1", "key2", "val2");
        long res = jedis.exists("key1", "key2");
        boolean res1 = jedis.exists("key1");
        System.out.println(res + " " + res1);
        res = jedis.del("key1", "key2"); // 返回删除了几个
        System.out.println(res);
    }
    public static void test3(Jedis jedis){
        jedis.flushDB();
        jedis.mset("key1", "1", "key2", "2", "key3", "3");
        Set<String> set = jedis.keys("*");
        System.out.println(set);
    }
    public static void test4(Jedis jedis){
        jedis.flushDB();
        jedis.set("key1", "1");
        jedis.expire("key1", 10);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long res = jedis.ttl("key1");
        System.out.println("ttl: "+res);
    }
    public static void test5(Jedis jedis){
        jedis.flushDB();
        jedis.set("key1", "1");
        String key1 = jedis.type("key1");
        System.out.println(key1);

        jedis.hset("key2", "f1", "1");
        String key2 = jedis.type("key2");
        System.out.println(key2);

        jedis.lpush("key3", "1", "2", "3");
        String key3 = jedis.type("key3");
        System.out.println(key3);

        jedis.sadd("key4", "111", "222");
        String key4 = jedis.type("key4");
        System.out.println(key4);

        jedis.zadd("key5", 1.0, "f1");
        String key5 = jedis.type("key5");
        System.out.println(key5);
    }
    public static void main(String[] args) {
        //连接到 redis 服务器
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()){
//            String res = jedis.ping();
//            System.out.println(res);
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
            test5(jedis);
        }
    }
}
